

function getLength(buffer) {
    // Payload length=x为0~125：数据的长度为x字节。
    // Payload length=x为126：后续2个字节代表一个16位的无符号整数，该无符号整数的值为数据的长度
    // Payload length=x为127：后续8个字节代表一个64位的无符号整数（最高位为0），该无符号整数的值为数据的长度
    const byte = buffer.readUInt8(0); //变成十进制
    let length = parseInt(byte.toString(2).substring(1), 2); //转换成二进制，并且截取后，又转换成十进制
    if (length === 126) {
        length = buffer.readUInt16BE(1);
    } else if (length === 127) {
        length = buffer.readBigUInt64BE(1);
    }
    return length;
}
console.log(126..toString(2)); //1111110
console.log(127..toString(2)); //1111111
console.log(getLength(Buffer.from([0b10000001, 0b10000001])));
console.log(getLength(Buffer.from([0b10000001, 0b11111110, 0b00000000, 0b00000001])));
console.log(getLength(Buffer.from([0b10000001, 0b11111111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001])));